﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Components;
using UI.Lib;
using UI.Lib.Factory;

namespace UI.BuyManagement
{
    public partial class FrmPurchaseReturnMedit : UI.ParentForm.BaseMedit
    {
        private bool isReadOnly = false;
        private ToolStripButton btnGetData = null;

        public FrmPurchaseReturnMedit(string modulecode, string action, string text, string key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            this.Key = key;

            //回写的方法
            this.SaveCallFuntion = "Sto8a00Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = false; //可能用户一次想把全部的记录删除
            this.MaxStateFlag = true;//打开页面后让窗口最大化
            if (!string.IsNullOrEmpty(this.Key))
            {
                IsAfterSaveCloseWin = true;
                LoadData();
            }
        }

        protected override void PageInit()
        {
            this.tbSummary.DefaultWidth = 130;

            if (false && this.Action == "A")
            {
                //加入提取采购单详情按钮
                btnGetData = CreateToolBtn("T", "提取采购单", BtnGetDataClick);
                this.tspDtlTop.Items.Insert(0, btnGetData);

                var resetBtn = CreateToolBtn("R", "重 置", ResetBtnClick);
                this.tspDtlTop.Items.Add(resetBtn);
            }
        }

        void BtnGetDataClick(object sender, EventArgs e)
        {
            if (this.Check() == false)
                return;

            this.tbSummary.Enable("refdocno", false);

            btnGetData.Enabled = false;

            var where = QB.Entity
                .AddParam("@storageid", this.tbSummary.GetValue("refid"))
                .AddParam("@comcode", Context.realComcode);

            var table = CommonHelper.Entity.ExecFun("getreturndatasource", where);
            this.BindEditData(table);
        }

        void ResetBtnClick(object sender, EventArgs e)
        {
            this.tbSummary.Enable("refdocno", true);
            if (btnGetData != null)
                btnGetData.Enabled = true;
            isReadOnly = false;
        }

        //新增行后触发，用户只读概要
        protected override void AfterAddRow()
        {
            if (isReadOnly == true) return;

            this.tbSummary.Enable("refdocno", false);
        }

        protected override void AfterSaveData(bool isSuccess)
        {
            if (isSuccess)
                ResetBtnClick(null, null);
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("refdocno" == name) //对销售商进行公司限制
                where = Context.GetAppKeyWhere("a.usercode", false);
            else if ("oprovidercode" == name)
                where = Context.GetAppKeyWhere("usercode", false);
            else if ("stocode" == name)
                where = Context.GetAppKeyWhere("usercode", false);

            return true;
        }

        protected override void AfterGridEndEdit(string colName, DataGridViewCellEventArgs e)
        {
            if ("dtlqty" == colName) //计算合计数量
            {
                CalcDtlQty();
                CalcTotalAmt();
            }
            else if ("dtlprice" == colName)
            {
                CalcTotalAmt();
            }
        }

        private void CalcTotalAmt()
        {
            double price = Validation.ToDouble(this.dgvDtl.GetValue("dtlprice")); //价格
            double ordqty = Validation.ToDouble(this.dgvDtl.GetValue("dtlqty")); //订货数量
            double all = price * ordqty; //还要算上税率

            this.dgvDtl.SetValue("dtltotalamt", all);
        }

        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.listid ='{0}' ", this.Key));
            base.LoadDtlData(null, string.Format(" a.dtldocid ='{0}' ", this.Key));
        }

        protected override void AfterDtlValueChanged(string name, object value)
        {
            if ("ordamt" == name) //订单总额变化后更新概要的订单总额
            {
                this.tbSummary.SetValue("ordamt", CalcDtlAmt());
            }
        }

        private double CalcDtlAmt()
        {
            double sum = 0;
            foreach (DataGridViewRow dr in this.dgvDtl.Rows)
                sum += Validation.ToDouble(dr.Cells["dtltotalamt"].Value);

            return sum;
        }

        private void CalcDtlQty()
        {
            double qty = 0;
            foreach (DataGridViewRow dr in this.dgvDtl.Rows)
                qty += Validation.ToDouble(dr.Cells["dtlqty"].Value);

            this.tbSummary.SetValue("totalqty", qty);
        }

        protected override void AfterDelete()
        {
            if (this.dgvDtl.Rows.Count == 0)
            {
                this.tbSummary.Enable("refdocno", true);
                isReadOnly = false;
            }

            CalcDtlQty();
            this.tbSummary.SetValue("ordamt", CalcDtlAmt());
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("productcode" == name)
            {
                where = string.Format("status ='1' {0} ", Context.GetAppKeyWhere("muser", true));
            }

            return true;
        }
    }
}
